package ATM.logic;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

import ATM.data.Accounts;
import ATM.data.AdminData;
import ATM.data.AtmData;
import ATM.data.Customers;
import ATM.data.KeyValue;
import ATM.gui.Screen;

public class AdminActionListener implements ActionListener {
	Screen screen;
	int counter = 0;
	int customerid = 0;
	String accType;
	AdminData adminData;
	ArrayList<Customers> customers;

	public AdminActionListener(Screen screen) {
		super();
		adminData = new AdminData(screen);
		this.screen = screen;
		customers = adminData.getCustomer();

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String ac = arg0.getActionCommand();

		if (ac.equals("MenuAdmin")) {
			screen.show("MENUADMIN");
		}
		if (ac.equals("NewCustomer")) {
			screen.show("NEWCUSTOMER");
		}

		else if (ac.equals("AddAccount")) {
			screen.show("ADDACCOUNT");

		} else if (ac.equals("Edit")) {
			EditCustomer();
		}

		else if (ac.equals("ViewCustomer")) {
			ViewCurrentCustomer(customers);
		}

		else if (ac.equals("NewCustomerFinish")) {
			NewCustomerAllFields();
		}

		else if (ac.equals("NextVC")) {
			NextPreviousViewCustomers("Next");

		}

		else if (ac.equals("PreviousVC")) {
			NextPreviousViewCustomers("Previous");
		}

		else if (ac.equals("SearchByCustomerID")) {
			customerid = Integer.parseInt(screen.getViewCustomer()
					.getTlfdsearch().getText());

			if (adminData.CustomerExits(customerid) == true) {
				ArrayList<Customers> customer = adminData
						.getCustomer(customerid);
				ViewSearchCustomer(customer);
			} else {
				JOptionPane.showMessageDialog(null, "Customer ID dosn't exist");
			}
		}

		else if (ac.equals("AccountList")) {
			JComboBox<String> cb = (JComboBox) arg0.getSource();
			String accountType = (String) cb.getSelectedItem();
			setInterest(accountType);
		}

		else if (ac.equals("AddThisAccount")) {
			AddAccount();

		} else if (ac.equals("DeleteCustomerAccount")) {
			DeleteCustomerAccount();
		} else if (ac.equals("DeleteAccount")) {
			if (JOptionPane.showConfirmDialog(null, "Delete account?") == JOptionPane.YES_OPTION)
				DeleteAccount();
		}

	}

	private void DeleteAccount() {
		int accountID = Integer.parseInt(((KeyValue) screen.getDeleteAccount()
				.getAccounts().getSelectedItem()).getValue());
		if (accountID > -1)
			adminData.DeleteAccount(accountID);
		DeleteCustomerAccount();
	}

	private void DeleteCustomerAccount() {
		screen.getDeleteAccount().setAccounts(
				adminData.getAccountsForCustomer(customers.get(counter)
						.getCustomerID()));
		screen.show("DELETEACCOUNT");
	}

	public void NextPreviousViewCustomers(String button) {
		if ("Next" == button) {
			if (counter + 1 < customers.size()) {
				counter++;
				ViewCurrentCustomer(customers);
			} else {
				counter = 0;
				ViewCurrentCustomer(customers);
			}
		} else {
			if (counter > 0) {
				counter--;
				ViewCurrentCustomer(customers);
			} else {
				counter = customers.size() - 1;
				ViewCurrentCustomer(customers);
			}
		}
	}

	public void AddAccount() {
		int customerid = Integer.parseInt(screen.getViewCustomer()
				.getTfldcustomerid().getText());
		double inRate = Double.parseDouble(screen.getAddAccount()
				.getTfldIntRateData().getText());
		double ovDraft = Double.parseDouble(screen.getAddAccount()
				.getTfldOvDraftData().getText());
		accType = ((JComboBox<String>) screen.getAddAccount().getAccountList())
				.getSelectedItem().toString();
		adminData.AddAccount(customerid, accType, inRate, ovDraft);
		ViewCurrentCustomer(customers);
	}

	public void NewCustomerAllFields() {
		if (screen.getNewCustomer().getTfldFirstName().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter First Name");
		} else if (screen.getNewCustomer().getTfldLastName().getText()
				.isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter Last Name");
		} else if (screen.getNewCustomer().getTfldCpr().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter CPR");
		} else if (screen.getNewCustomer().getTfldAddress().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter Address");
		} else if (screen.getNewCustomer().getTfldCity().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter City");
		} else if (screen.getNewCustomer().getTfldZipCode().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter zip code");
		} else if (screen.getNewCustomer().getTfldCountry().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter country");
		} else if (screen.getNewCustomer().getTfldNumber().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter number");
		} else if (screen.getNewCustomer().getTfldMail().getText().isEmpty()) {
			JOptionPane.showMessageDialog(null, "Enter main");
		} else {
			NewCustomer();
		}

	}

	public void NewCustomer() {
		int customerID = adminData.newCustomerID();
		String firstName = screen.getNewCustomer().getTfldFirstName().getText();
		String lastName = screen.getNewCustomer().getTfldLastName().getText();
		String cpr = screen.getNewCustomer().getTfldCpr().getText();
		String address = screen.getNewCustomer().getTfldAddress().getText();
		String city = screen.getNewCustomer().getTfldCity().getText();
		int zipCode = Integer.parseInt(screen.getNewCustomer().getTfldZipCode()
				.getText());
		String country = screen.getNewCustomer().getTfldCountry().getText();
		int phonenumber = Integer.parseInt(screen.getNewCustomer()
				.getTfldNumber().getText());
		String mail = screen.getNewCustomer().getTfldMail().getText();
		adminData.addCustomer(customerID, firstName, lastName, cpr, address,
				city, zipCode, country, phonenumber, mail);
		screen.show("VIEWCUSTOMER");
		ArrayList<Customers> customer = adminData.getCustomer(customerID);
		ViewSearchCustomer(customer);
	}

	public void EditCustomer() {
		ArrayList<Customers> customer = screen.getViewCustomer().EditCustomer();
		if (adminData.EditCustomer(customer)) {
			JOptionPane.showMessageDialog(screen,
					"The customer has been edited");
			customers = adminData.getCustomer();
			ViewCurrentCustomer(customers);
		} else {
			JOptionPane.showMessageDialog(null, "The customer was not edited");
		}

	}

	public void ViewCurrentCustomer(ArrayList<Customers> customers) {
		screen.show("VIEWCUSTOMER");
		String customerid = Integer.toString(customers.get(counter)
				.getCustomerID());
		String password = Integer
				.toString(customers.get(counter).getPassword());
		String firstname = customers.get(counter).getFirstName();
		String lastname = customers.get(counter).getLastName();
		String cpr = customers.get(counter).getCpr();
		String address = customers.get(counter).getAddress();
		String zipcode = Integer.toString(customers.get(counter).getZipCode());
		String city = customers.get(counter).getCity();
		String country = customers.get(counter).getCountry();
		String phone = Integer.toString(customers.get(counter).getNumber());
		String mail = customers.get(counter).getMail();
		screen.getViewCustomer().setTextField(customerid, password, firstname,
				lastname, cpr, address, zipcode, city, country, phone, mail);

	}

	public void ViewSearchCustomer(ArrayList<Customers> customer) {
		String customerid = Integer.toString(customer.get(0).getCustomerID());
		String password = Integer.toString(customer.get(counter).getPassword());
		String firstname = customer.get(counter).getFirstName();
		String lastname = customer.get(counter).getLastName();
		String cpr = customer.get(counter).getCpr();
		String address = customer.get(counter).getAddress();
		String zipcode = Integer.toString(customer.get(counter).getZipCode());
		String city = customer.get(counter).getCity();
		String country = customer.get(counter).getCountry();
		String phone = Integer.toString(customer.get(counter).getNumber());
		String mail = customer.get(counter).getMail();
		screen.getViewCustomer().setTextField(customerid, password, firstname,
				lastname, cpr, address, zipcode, city, country, phone, mail);

	}

	public void setInterest(String accountType) {
		if (accountType == "Main Account") {
			screen.getAddAccount().getTfldIntRateData().setText("4");
			screen.getAddAccount().getTfldIntRateData().setEditable(false);
			accType = accountType;
		}
		if (accountType == "Savings") {
			screen.getAddAccount().getTfldIntRateData().setText("2");
			screen.getAddAccount().getTfldIntRateData().setEditable(false);
			accType = accountType;
		}
		if (accountType == "Other") {
			screen.getAddAccount().getTfldIntRateData().setEditable(true);
			accType = accountType;
		}
	}
}
